API reference¶
API reference for TTP module.
-
class
ttp.
ttp
(data='', template='', log_level='WARNING', log_file=None, base_path='', vars={})¶ Template Text Parser main class to load data, templates, lookups, variables and dispatch data to parser object to parse in single or multiple processes, construct final results and run outputs.
Parameters
data
file object or OS path to text file or directory with text files with data to parsetemplate
file object or OS path to text file with templatebase_path
(str) base OS path prefix to load data from for template’s inputslog_level
(str) level of logging “DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”log_file
(str) path where to save log filevars
dictionary of variables to make available to ttp parser
Example:
from ttp import ttp parser = ttp(data="/os/path/to/data/dir/", template="/os/path/to/template.txt") parser.parse() result = parser.result(format="json") print(result[0])
-
add_input
(data, input_name='Default_Input', groups=['all'])¶ Method to load additional data to be parsed. This data will be used to fill in template input with input_name and parse that data against a list of provided groups.
Parameters
data
file object or OS path to text file or directory with text files with data to parseinput_name
(str) name of the input to put data in, default is Default_Inputgroups
(list) list of group names to use to parse this input data
-
add_lookup
(name, text_data='', include=None, load='python', key=None)¶ Method to add lookup table data to all templates loaded so far. Lookup is a text representation of structure that can be loaded into python dictionary using one of the available loaders - python, csv, ini, yaml, json.
Parameters
name
(str) name to assign to this lookup table to reference in templatestext_data
(str) text to load lookup table/dictionary frominclude
(str) absolute or relative /os/path/to/lookup/table/file.txtload
(str) name of TTP loader to use to load table datakey
(str) specify key column for csv loader to construct dictionary
include
can accept relative OS path - relative to the directory where TTP will be invoked either using CLI tool or as a module
-
add_template
(template, template_name=None)¶ Method to load TTP templates into the parser.
Parameters
template
file object or OS path to text file with templatetemplate_name
(str) name of the template
-
add_vars
(vars)¶ Method to add variables to ttp and its templates to reference during parsing
Parameters
vars
dictionary of variables to make available to ttp parser
-
clear_input
()¶ Method to delete all input data for all templates, can be used prior to adding new set of data to parse with same templates, instead of re-initializing ttp object.
-
get_input_commands_dict
()¶ Method to iterate over all templates and inputs to get a list of commands that needs to be present in text data, commands retrieved from input commands attribute.
Returns
Dictionary of {“input_name”: [input commands list]} across all templates, where input_name set to input name attribute value, by default it is “Default_Input”
Warning
inputs with the same name will override one another, make sure input name attribute is unique across all loaded templates.
-
get_input_commands_list
()¶ Method to iterate over all templates and inputs to get a list of commands that needs to be present in text data, commands retrieved from input commands attribute.
Returns
List of unique commands - [command1, command2, … , commandN]
-
get_input_load
()¶ Method to retrieve input tag text. If input’s load attribute was given, text data will be loaded into python structure using one of the loaders, for instance if text data structured using YAML, YAML loader can be used to produce python native structure, that structure will be returned by this method.
Primary use case is to specify parameters within TTP input that can be used by other applications/scrips.
Returns
Dictionary of {“input_name”: “input load data”} across all templates, where input_name set to input name attribute value, by default it is “Default_Input”
Warning
inputs with same names will override one another, make sure input name attribute is unique across all templates.
-
parse
(one=False, multi=False)¶ Method to parse data with templates.
Parameters
one
(boolean) if set to True will run parsing in single processmulti
(boolean) if set to True will run parsing in multiprocess
By default one and multi set to False and TTP will run parsing following below rules:
- if one or multi set to True run in one or multi process
- if overall data size is less then 5Mbyte, use single process
- if overall data size is more then 5Mbytes, use multiprocess
In addition to 3 TTP will check if number of input data items more then 1, if so multiple processes will be used and one process otherwise.
-
result
(templates=[], structure='list', **kwargs)¶ Method to get parsing results, supports basic filtering based on templates’ names, results can be formatted and returned to specified returner.
Parameters
templates
(list or str) names of the templates to return results forstructure
(str) structure type, valid values -list
ordictionary
kwargs - can contain any attributes supported by output tags, for instance:
format
(str) output formatter name - yaml, json, raw, pprint, csv, table, tabulatefunctions
(str) reference output functions to run results through
Example:
from ttp import ttp parser = ttp(data="/os/path/to/data/dir/", template="/os/path/to/template.txt") parser.parse() json_result = parser.result(format="json")[0] yaml_result = parser.result(format="yaml")[0] print(json_result) print(yaml_result)
Returns
By default template results set to per_input and structure set to list, returns list such as:
[ [ template_1_input_1_results, template_1_input_2_results, ... template_1_input_N_results ], [ template_2_input_1_results, template_2_input_2_results, ... ]
If template results set to per_template and structure set to list, returns list such as:
[ [ template_1_input_1_2...N_joined_results ], [ template_2_input_1_2...N_joined_results ] ]
If template results set to per_input and structure set to dictionary, returns dictionary such as:
{ template_1_name: [ input_1_results, input_2_results, ... input_N_results ], template_2_name: [ input_1_results, input_2_results ], ... }
If template results set to per_template and structure set to dictionary, returns dictionary such as:
{ template_1_name: input_1_2...N_joined_results, template_2_name: input_1_2...N_joined_results }
-
set_input
(data, input_name='Default_Input', groups=['all'])¶ Method to replace existing templates data with new set of data. This method run clear_input first and add_input method after that.
Parameters
data
file object or OS path to text file or directory with text files with data to parseinput_name
(str) name of the input to put data in, default is Default_Inputgroups
(list) list of group names to use to parse this input data